"""doc"""

from django.http import HttpRequest

from app_common import app_except

from . import models


def need_wx_login(func):
    """need wx login check"""

    def d(*args, **kwargs):
        request: HttpRequest = kwargs.get("request", None) or args[0]
        openid = request.META["HTTP_X_WX_OPENID"]
        _user = (
            models.WxUser.objects.filter(deleted=False).filter(openid=openid).first()
        )
        if not _user:
            raise app_except.WxNotRegister

        request.wxuser = _user
        return func(*args, **kwargs)

    return d


try_and_need_wx_login_decorators = [app_except.app_try, need_wx_login]
try_decorators = [app_except.app_try]
